// EvaluatingPlayer.cpp : implementation of the CEvaluatingPlayer class
//
#include "EvaluatingPlayer.h"
#include "math.h" 

// Destruktor
CEvaluatingPlayer::~CEvaluatingPlayer()
{
	RemoveAllContributionsFromPanes();
}

// Nove policko sa vytvori, naplni sa a vlozi do zoznamu
void CEvaluatingPlayer::InsertPaneToPaneList(CEvaluatingPane* ePane, double lfContribution)
{
	PaneList* paneList = new PaneList;
	paneList->ePane =  ePane;
	paneList->lfContribution = lfContribution;

	influencePaneList.push_back(paneList);
}

// Vypocita svoj prispevok na policko nachadzajuce sa nRadius policok od hraca
double CEvaluatingPlayer::CalculatePlayerContribution(CEvaluatingPane* ePane, int nRadius)
{
	// ZAVISLOST VPLYVU HRACA OD JEHO VZDIALENOSTI, BUDE ZATIAL MATEMATICKA LINEARNA FUNKCIA Y = 1 - X
	// NESKOR SA MOZE VYSKUSAT NAPR. EXPONENCIALNA FUNKCIA

	double lfPaneSize = sqrt(ePane->GetPaneHeight() * ePane->GetPaneWidth());

	// Prirastok na 1 policko
	double lfDelta = ((double)GetPlayerInfluencePower() / (double)GetPlayerInfluenceSize()) * lfPaneSize;
	// Zakladny prirastok hraca. Este neuvazujeme nad jeho natocenim
	double lfBasicContr = GetPlayerInfluencePower() - nRadius * lfDelta;
	
	const double pi = 3.1415926535;

	// Najskor natocime poziciu policka o minus uhol natocenia hraca
	double a = (ePane->GetPaneCenterX() - GetPlayerXPosition());
	double b = (ePane->GetPaneCenterY() - GetPlayerYPosition());

	double lfInfRadAngle = atan2(b,a);

	double lfRadius = sqrt(pow(b, 2) + pow(a, 2));
	
	double lfRadAngle = lfInfRadAngle - GetPlayerTurnAngle() * pi /180;

	if(lfRadAngle >= pi)
		lfRadAngle -= (2*pi);
	else if(lfRadAngle <= -pi)
		lfRadAngle += (2*pi);

	double lfDegAngle = lfRadAngle * 180 / pi;
	
	// Policko, na ktorom stoji hrac
	if(lfRadius == 0)
	{
		InsertPaneToPaneList(ePane, lfBasicContr);	
		return lfBasicContr;
	}

	double lfPlayerContr;

	if(fabs(lfDegAngle) <= 60)
		lfPlayerContr = lfBasicContr * cos(fabs(lfRadAngle));
	else if(fabs(lfDegAngle) <= 90)
		lfPlayerContr = lfBasicContr * cos(60 * pi / 180);
	else
		lfPlayerContr = lfBasicContr * cos(60 * pi / 180) / 2.0;
	
	if(GetPlayerType() != PT_OUR)
		lfPlayerContr = -lfPlayerContr;

	InsertPaneToPaneList(ePane, lfPlayerContr);	

	return lfPlayerContr;
}

// Spocita prispevky vsetkych policok pod vplyvom hraca
double CEvaluatingPlayer::CalculateMaxEvaluation()
{
	double lfAreaEv = 0.0;

	for(influencePaneListIterator = GetInflPaneListIteratorBegin(); influencePaneListIterator != GetInflPaneListIteratorEnd(); influencePaneListIterator++)
	{	
			
		CEvaluatingPane* ePane = (*influencePaneListIterator)->ePane;
		lfAreaEv += (ePane->GetPaneValue() - ePane->GetOriginalPaneValue());
	}

	return lfAreaEv;
}


// Odstrani prispevky tohto hraca zo vsetkych policok
void CEvaluatingPlayer::RemoveAllContributionsFromPanes()
{
	for(influencePaneListIterator = GetInflPaneListIteratorBegin(); influencePaneListIterator != GetInflPaneListIteratorEnd(); influencePaneListIterator++)
	{	
			
		PaneList* paneList = (*influencePaneListIterator);

		(paneList->ePane)->RemovePlayerContribution(paneList->lfContribution);
	
		delete (*influencePaneListIterator);
	}

	influencePaneList.clear();
}
